package com.wushengqi.camera;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.FrameLayout;

import com.wushengqi.camera.view.CameraPreview;

public class MainActivity extends AppCompatActivity {

    private String[] permissionArr = new String[]{
            Manifest.permission.CAMERA
    };

    public static final int CAMARE_REQUST_CODE = 10086;

    private FrameLayout preview;

    private CameraPreview cameraPreview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        preview = findViewById(R.id.preview);

        requestCamera();
    }

    private void requestCamera(){
        for (String pms: permissionArr){
            if(ContextCompat.checkSelfPermission(this, pms) == PackageManager.PERMISSION_GRANTED){
                cameraPreview.start();
            }else{
                ActivityCompat.requestPermissions(this, permissionArr, CAMARE_REQUST_CODE);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == CAMARE_REQUST_CODE){
            cameraPreview.start();
        }
    }
}
